package com.ruoyi.web.controller.api.shop;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.system.domain.PStoreCart;
import com.ruoyi.system.domain.bo.PStoreCartBo;
import com.ruoyi.system.domain.vo.CartInfoResponse;
import com.ruoyi.system.service.IPStoreCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Arrays;

/**
 * @Description:
 * @Author: wk
 * @Date: 2022/8/8 8:08
 * @Version: 1.0
 */

@Api(tags = {"前端-购物车管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/cart")
public class CartApiController {
    private final IPStoreCartService iPStoreCartService;

    /**
     * 查询购物车列表
     */
    @ApiOperation("查询购物车列表")
    @GetMapping("/list")
    public TableDataInfo<CartInfoResponse> list(PStoreCartBo bo, PageQuery pageQuery) {
        return iPStoreCartService.queryPageFontList(bo, pageQuery);
    }

    /**
     * 新增购物车
     */
    @ApiOperation("新增购物车")
    @RepeatSubmit()
    @PostMapping("/add")
    public R<String> add(@Validated(AddGroup.class) @RequestBody PStoreCartBo bo) {
        return R.ok(iPStoreCartService.insertFontByBo(bo));
    }

    @ApiOperation("删除购物车")
    @GetMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        iPStoreCartService.deleteWithValidByIds(Arrays.asList(ids), true);
        return R.ok();
    }

    @ApiOperation("修改商品数量")
    @GetMapping("/edit-count")
    public R<Void> edit(@RequestParam("id") Long id, @RequestParam("number") Integer number) {
        iPStoreCartService.updateByFontBo(id, number);
        return R.ok();
    }

    @ApiOperation("获取购物车数量")
    @GetMapping("/count")
    public R<Long> count() {
        return R.ok(iPStoreCartService.getCount());
    }

    @ApiOperation("购物车重选提交")
    @RepeatSubmit()
    @PutMapping("/reset-cart")
    public R<Void> resetCart(@Validated(EditGroup.class) @RequestBody PStoreCartBo bo) {
        iPStoreCartService.resetCart(bo);
        return R.ok();
    }

    @ApiOperation("商城购物车列表")
    @GetMapping("/cartList")
    public TableDataInfo<PStoreCart> cartList(PageQuery pageQuery) {
        return iPStoreCartService.cartList(pageQuery);
    }
}
